package com.vchello.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 定制解析或者组装Json
 * @author lmq
 *
 */
public class JsonUtils {
	
	/**
     * 将json转化为实体POJO
     * @param jsonStr
     * @param obj
     * @return
     */
    public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
        T t = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            t = objectMapper.readValue(jsonStr,
                    obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
    
    /**
     * 将实体POJO转化为JSON
     * @param obj
     * @return
     * @throws JSONException
     * @throws IOException
     */
    public static<T> JSONObject objectToJson(T obj) throws JSONException, IOException {
        ObjectMapper mapper = new ObjectMapper();  
        // Convert object to JSON string  
        String jsonStr = "";
        try {
             jsonStr =  mapper.writeValueAsString(obj);
        } catch (IOException e) {
            throw e;
        }
        return new JSONObject(jsonStr);
    }

	/**
	 * 返回的结果解析为map
	 * @param result
	 * @return
	 */
	public static Map<String, String> bussCardToMap(String result){
        // 解析请求结果
		Map<String, String> map =  null;
        try {
            JSONObject result_obj = new JSONObject(result);
            JSONArray output_array = result_obj.getJSONArray("outputs");
            // 取出结果json字符串
            String output = output_array.getJSONObject(0)
                    .getJSONObject("outputValue").getString("dataValue"); //请注意，dataValue字段对应的是一个字符串，不是json object
            JSONObject out = new JSONObject(output);
            if (out.getBoolean("success")) {
            	map = new HashMap<String, String>();
                // code to get result
                // 获取姓名, 只有姓名不是数组
            	String request_id_array = out.getString("request_id");
                String name = out.getString("name");
                map.put("name", name);
                // 获取所有公司结果
                JSONArray company_array = out.getJSONArray("company");
                // 获取手机号数组
                JSONArray tel_cell_array = out.getJSONArray("tel_cell");
                // 获取邮箱数组
                JSONArray email_array = out.getJSONArray("email");
                // 获取头衔数组
                JSONArray title_array = out.getJSONArray("title");
                // 请求对应的唯一表示，可以用于事后debug问题
                //即使有多个，也获取第一个而已。
                if(company_array.length()>0){
                	map.put("company", company_array.getString(0));
                }else{
                	map.put("company", "");
                }
                if(tel_cell_array.length()>0){
                	map.put("phone", tel_cell_array.getString(0));
                }else{
                	map.put("phone", "");
                }
                if(email_array.length()>0){
                	map.put("email", email_array.getString(0));
                }else{
                	map.put("email", "");
                }
                if(title_array.length()>0){
                	map.put("title", title_array.getString(0));
                }else{
                	map.put("title", "");
                }
            } else {
                System.out.println("predict error");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
		return map;
	}
	
	public static String bussCardMapToJson(Map<String, String> map, String path) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("uploadFile", path);
		jsonObject.put("status", true);
		jsonObject.put("company", map.get("company"));
		jsonObject.put("phone", map.get("phone"));
		jsonObject.put("email", map.get("email"));
		jsonObject.put("title", map.get("title"));
		jsonObject.put("name", map.get("name"));
		return jsonObject.toString();
	}
	
}
